/**
 * 
 */
package com.zxc.bytecode;

import java.math.BigDecimal;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import com.zxc.utils.Utility;

/**
 * @author Administrator
 * 
 */
public class HelloScriptingWorld {
	public static void main(String[] args) throws ScriptException,
			NoSuchMethodException {

		ScriptEngineManager scriptEngineMgr = new ScriptEngineManager();
		ScriptEngine jsEngine = scriptEngineMgr.getEngineByName("JavaScript");

		if (jsEngine == null) {
			System.err.println("No script engine found for JavaScript");
			System.exit(1);
		}

		System.out.println("Calling invokeHelloScript...");
		invokeHelloScript(jsEngine);

		System.out.println("\nCalling defineScriptFunction...");
		defineScriptFunction(jsEngine);

		System.out.println("\nCalling invokeScriptFunctionFromEngine...");
		invokeScriptFunctionFromEngine(jsEngine);

		System.out.println("\nCalling invokeScriptFunctionFromJava...");
		invokeScriptFunctionFromJava(jsEngine);

		System.out.println("\nCalling invokeJavaFromScriptFunction...");
		invokeJavaFromScriptFunction(jsEngine);
	}

	private static void invokeHelloScript(ScriptEngine jsEngine)
			throws ScriptException {
		jsEngine.eval("println('Hello from JavaScript')");
	}

	private static void defineScriptFunction(ScriptEngine engine)
			throws ScriptException {
		// Define a function in the script engine
		engine.eval("function sayHello(name) {"
				+ "    println('Hello, ' + name)" + "}");
	}

	private static void invokeScriptFunctionFromEngine(ScriptEngine engine)
			throws ScriptException {
		engine.eval("sayHello('World!')");
	}

	private static void invokeScriptFunctionFromJava(ScriptEngine engine)
			throws ScriptException, NoSuchMethodException {
		Invocable invocableEngine = (Invocable) engine;
		invocableEngine.invokeFunction("sayHello", "from Java");
	}

	private static void invokeJavaFromScriptFunction(ScriptEngine engine)
			throws ScriptException {
		HelloScriptingWorld obj = new HelloScriptingWorld();
		engine.put("helloScriptingWorld", obj);
		/*engine.eval("println('Invoking getHelloReply method from JavaScript...');"
				+ "var msg = helloScriptingWorld.getHelloReply('vJavaScript');"
				+ "println('Java returned: ' + msg)");*/
		engine.eval("println('Invoking setHelloReply method from JavaScript...');"
				+ "helloScriptingWorld.setHelloReply('vJavaScript');"
				+ "var msg = helloScriptingWorld.compareNum();"
				+ "println('Java returned: ' + msg)");
		System.out.println(obj.getName());
	}

	/** Method invoked from the above script to return a string. */
	public String getHelloReply(String name) {
		return "Java method getHelloReply says, 'Hello, " + name + "'";
	}
	
	private String name;
	
	private BigDecimal num1 = BigDecimal.valueOf(1);
	
	private BigDecimal num2 = BigDecimal.valueOf(1);
	
	public boolean compareNum(){
		return Utility.isEquals(num1, num2);
	}
	
	public void setHelloReply(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
